package test8;

import java.util.Arrays;


// Comparable接口
class Student implements Comparable<Student>{
    public int age;
    public String name;
    public double score;

    public Student(int age, String name, double score) {
        this.age = age;
        this.name = name;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", score=" + score +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        //谁调用compareTo谁就是this
//        if(this.age > o.age){
//            return 1;
//        }else if(this.age == o.age){
//            return 0;
//        }else{
//            return -1;
//        }
        return this.age-o.age;
    }
}
public class Demo8 {

    public static void main2(String[] args) {
        Student student1=new Student(2,"aaa",98.9);
        Student student2=new Student(2,"abc",89.9);
//        if(student1.compareTo(student2) > 0){
//
//        }
        System.out.println(student1.compareTo(student2));
    }


    public static void main(String[] args) {
        Student[] students=new Student[3];
        students[0]=new Student(12,"aaa",98.9);
        students[1]=new Student(62,"abc",89.9);
        students[2]=new Student(18,"zhangsan",18.9);
        System.out.println(Arrays.toString(students));
        Arrays.sort(students);
        System.out.println(Arrays.toString(students));

    }
}
